#include "isr.h"

uint32_t get_cr2();

uint32_t exception_handler(registers_t* regs){
	printk("exception");
	while(1);
	return 0;
}

uint32_t exception_handler0(registers_t* regs){
	printk("cero division");
	while(1);
	return 0;
}

uint32_t exception_handler1(registers_t* regs){
	printk("Debug");
	while(1);
	return 0;
}

uint32_t exception_handler3(registers_t* regs){
	printk("Breakpoint");
	while(1);
	return 0;
}


uint32_t exception_handler4(registers_t* regs){
	printk("overflow");
	while(1);
	return 0;
}

uint32_t exception_handler5(registers_t* regs){
	printk("Bound Range Exceeded");
	while(1);
	return 0;
}

uint32_t exception_handler6(registers_t* regs){
	printk("Invalid Opcode");
	while(1);
	return 0;
}		 

uint32_t exception_handler7(registers_t* regs){
	printk("Device Not Available");
	while(1);
	return 0;
}	

uint32_t exception_handler8(registers_t* regs){
	printk("Double Fault");
	while(1);
	return 0;
}	

uint32_t exception_handler9(registers_t* regs){
	printk("Coprocessor Segment Overrun");
	while(1);
	return 0;
}
	

uint32_t exception_handler10(registers_t* regs){
	printk("Invalid TSS\n");
	unsigned int s2=regs->err_code;
	
	int j;
	
	for(j=0;j<32;j++){
		if(s2%2==1){
			printk("1");
		}
		else{
			printk("0");
		}
		s2/=2;
	}
	while(1);
	return 0;
}

uint32_t exception_handler11(registers_t* regs){
	printk("Segment Not Present");
	while(1);
	return 0;
}

uint32_t exception_handler12(registers_t* regs){
	printk("Stack-Segment Fault");
	while(1);
	return 0;
}

uint32_t exception_handler13(registers_t* regs){
	printk("General Protection Fault");
	unsigned int s2=regs->err_code;
	
	int j;
	
	for(j=0;j<32;j++){
		if(s2%2==1){
			printk("1");
		}
		else{
			printk("0");
		}
		s2/=2;
	}
	
	while(1);
	return 0;
}

uint32_t exception_handler14(registers_t* regs){	
	extended_registers_t * ext_regs=(extended_registers_t*) regs;
	printk("cr2:%d\n",(int)((unsigned)get_cr2()/2));
	int j;
	unsigned s2=regs->err_code;
	
	for(j=0;j<32;j++){
		if(s2%2==1){
			printk("1");
		}
		else{
			printk("0");
		}
		s2/=2;
	}
	printk("page fault\n");
	while(1);
	return 0;
}


uint32_t exception_handler16(registers_t* regs){
	printk("x87 Floating-Point Exception");
	while(1);
	return 0;
}

uint32_t exception_handler17(registers_t* regs){
	printk("Alignment Check");
	while(1);
	return 0;
}

uint32_t exception_handler18(registers_t* regs){
	printk("Machine Check");
	while(1);
	return 0;
}

uint32_t exception_handler19(registers_t* regs){
	printk("SIMD Floating-Point Exception");
	while(1);
	return 0;
}

uint32_t exception_handler30(registers_t* regs){
	printk("Security Exception");
	while(1);
	return 0;
}


void init_exceptions(){
	int i;
	for(i=0;i<32;i++){
		register_interrupt_handler(i,exception_handler);
	}
	
	register_interrupt_handler(0,exception_handler0);
	register_interrupt_handler(1,exception_handler1);
	register_interrupt_handler(3,exception_handler3);
	register_interrupt_handler(4,exception_handler4);
	register_interrupt_handler(5,exception_handler5);
	register_interrupt_handler(6,exception_handler6);
	register_interrupt_handler(7,exception_handler7);
	register_interrupt_handler(8,exception_handler8);
	register_interrupt_handler(9,exception_handler9);
	register_interrupt_handler(10,exception_handler10);
	register_interrupt_handler(11,exception_handler11);
	register_interrupt_handler(12,exception_handler12);
	register_interrupt_handler(13,exception_handler13);
	register_interrupt_handler(14,exception_handler14);
	register_interrupt_handler(16,exception_handler16);
	register_interrupt_handler(17,exception_handler17);
	register_interrupt_handler(18,exception_handler18);
	register_interrupt_handler(19,exception_handler19);
	register_interrupt_handler(30,exception_handler30);
}
